#!/bin/sh

if [ "${2}" != "POST-INSTALL" ]; then
	exit 0
fi

if [ ! -f ${PKG_ROOTDIR}/usr/local/share/%%PRODUCT_NAME%%/base.txz ]; then
	echo "===> ERROR: base tarball not found!"
	exit 1
fi

# Save a copy of /etc/version to trigger copynotice.inc show when it's needed
if [ ! -f ${PKG_ROOTDIR}/cf/conf/copynotice_version ]; then
	# Create empty file to force it to differ and show notice once
	echo "" > ${PKG_ROOTDIR}/cf/conf/copynotice_version
else
	/bin/cp -f ${PKG_ROOTDIR}/etc/version ${PKG_ROOTDIR}/cf/conf/copynotice_version
fi

echo %%PRODUCT_NAME%% > ${PKG_ROOTDIR}/etc/platform

echo "===> Removing schg flag from base files"

# Cleanup schg flags
chflags -R noschg \
	${PKG_ROOTDIR}/boot \
	${PKG_ROOTDIR}/bin \
	${PKG_ROOTDIR}/sbin \
	${PKG_ROOTDIR}/usr/bin \
	${PKG_ROOTDIR}/usr/sbin \
	${PKG_ROOTDIR}/libexec \
	${PKG_ROOTDIR}/lib \
	${PKG_ROOTDIR}/usr/lib >/dev/null 2>&1

echo "===> Extracting new base tarball"

# Install new base files
tar -C ${PKG_ROOTDIR}/ --exclude ./var/empty -xJPUf \
	/usr/local/share/%%PRODUCT_NAME%%/base.txz

# Create/remove trigger to show copynotice when version changes
if /usr/bin/cmp -s ${PKG_ROOTDIR}/etc/version ${PKG_ROOTDIR}/cf/conf/copynotice_version; then
	/bin/rm -f ${PKG_ROOTDIR}/cf/conf/copynotice_display >/dev/null 2>&1
else
	/usr/bin/touch ${PKG_ROOTDIR}/cf/conf/copynotice_display
fi

echo "===> Removing static obsoleted files"

# Set IFS to \n to deal with filenames containing spaces
oIFS=${IFS}
IFS="
"

# Process obsolete files
if [ -f ${PKG_ROOTDIR}/etc/%%PRODUCT_NAME%%.obsoletedfiles ]; then
	for f in $(cat ${PKG_ROOTDIR}/etc/%%PRODUCT_NAME%%.obsoletedfiles); do
		if [ -n "${f}" -a -d "${PKG_ROOTDIR}${f}" ]; then
			chflags -R noschg "${PKG_ROOTDIR}${f}"
			rm -rf "${PKG_ROOTDIR}${f}"
		elif [ -n "${f}" -a -f "${PKG_ROOTDIR}${f}" ]; then
			chflags noschg "${PKG_ROOTDIR}${f}"
			rm -f "${PKG_ROOTDIR}${f}"
		elif [ -n "${f}" -a -L "${PKG_ROOTDIR}${f}" ]; then
			rm -f "${PKG_ROOTDIR}${f}"
		fi
	done
fi

# Restore IFS
IFS=${oIFS}

# Save current pkg_set_version
cp -f ${PKG_ROOTDIR}/etc/version ${PKG_ROOTDIR}/usr/local/share/%%PRODUCT_NAME%%/running_pkg_set_version

exit 0
